package com.cloud.wms.ui.main.presnter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.main.contract.BaseInfoContract;
import com.cloud.wms.ui.main.entity.MemberEntity;
import com.cloud.wms.ui.main.entity.RemoteEntity;
import com.cloud.wms.ui.main.entity.TaskMenuEntity;
import com.cloud.wms.ui.main.entity.UserEntity;
import com.cloud.wms.ui.main.entity.WarehouseEntity;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;

/**
 * @Author:Chen
 * @Date: 2021-01-14 15:51
 * @Description:
 */
public class BaseInfoPresenter extends BaseRxPresenter<BaseInfoContract.View> implements BaseInfoContract.Presenter {
    public static final int WAREHOUSE = 0x100;
    public static final int MEMBER = 0x101;
    public static final int STRATEGY = 0x102;
    public static final int TASK_MENU = 0x103;

    @Inject
    public BaseInfoPresenter() {

    }

    @Override
    public void loadWarehouseList() {
        Params params = new Params();
        params.put("UserID", FineExApplication.component().loginCache().getUserInfo().getUserID());
        FineExApplication
                .component()
                .http()
                .getWarehouseList(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<ArrayList<WarehouseEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(ArrayList<WarehouseEntity> info) {
                        if (info == null || info.isEmpty()) {
                            mView.showEmpty();
                            mView.info("无权限范围内的仓库");
                        } else {
                            mView.showContent();
                            mView.onSuccess(MessageCreator.createMessage(info, WAREHOUSE));
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                        mView.showError();
                    }
                });
    }

    @Override
    public void loadMemberList(long warehouseID, boolean isShow) {
        Params params = new Params();
        params.put("WarehouseID", warehouseID);
        FineExApplication
                .component()
                .http()
                .getMemberList(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<ArrayList<MemberEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(ArrayList<MemberEntity> info) {
//                        if (info == null || info.isEmpty()) {
//                            if (isShow)
//                                mView.showEmpty();
//                            mView.info("无权限范围内的会员");
//                        } else {
//                            if (isShow)
//                                mView.showContent();
                            mView.onSuccess(MessageCreator.createMessage(info, MEMBER));
//                        }
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                        if (isShow)
                            mView.showError();
                    }
                });
    }

    @Override
    public void loadStrategyList() {
        Observable.timer(200, TimeUnit.MILLISECONDS)
                .map(aLong -> {
                    ArrayList<Object> objects = new ArrayList<>();
                    return objects;
                })
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<ArrayList<Object>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(ArrayList<Object> info) {
                        mView.onSuccess(MessageCreator.createMessage(info, STRATEGY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    @Override
    public void loadMainTaskMenu() {
        Params params = new Params();
        UserEntity userInfo = FineExApplication.component().loginCache().getUserInfo();
        params.put("JobStationID", userInfo.getJobStationID());
        params.put("ProductCategory", userInfo.getProductCategory());

        FineExApplication
                .component()
                .http()
                .taskMenuList(params)
                .map(new ResponseWrapper<>())
                .map(RemoteEntity::getItems)
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<ArrayList<TaskMenuEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(ArrayList<TaskMenuEntity> info) {
                        mView.onSuccess(MessageCreator.createMessage(info, TASK_MENU));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
